class Friends {
	friendsList = []
	notificationList = []
	userList = []
	groupList = []
	groupUsers = []
	notificationUnreadCount = 0
	async getFriendsCmdMethods() {
		const {
			data
		} = await uni.$Im.executeCmdMethods(34, {});
		this.set(data);
		return data
	}
	/**
	 * 好友通知
	 * @param {Object} params
	 * 
	 */
	async getNotificationListCmdMethods(params) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(28, params);
		this.updateNotificationList(data);
		this.updateNotificationUnreadCount();
		return data;
	}

	getNotificationList(params) {
		return this.notificationList;
	}

	updateNotificationUnreadCount() {
		this.notificationUnreadCount = this.notificationList.reduce((total, item) => {
			item.isRead == 0 && total++
			return total
		}, 0);
	}

	getNotificationUnreadCount() {
		return this.notificationUnreadCount;
	}

	updateNotificationList(data) {
		this.notificationList = data.map(item => {
			return {
				...item,
				// isRead: 0,
				fromInfo: item.fromInfo && JSON.parse(item.fromInfo),
			}
		});
	}
	/**
	 * 添加好友
	 * @param {Object} params
	 * 
	 */
	async addFriendInvite(params) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(29, params);
	}
	/**
	 * 同意好友
	 * @param {Object} params
	 * 
	 */
	async acceptFriendApplication(params, callback) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(30, params);
		callback && callback(data);
		return data;
	}
	/**
	 *拒绝好友
	 * @param {Object} params
	 * 
	 */
	async refuseFriendApplication(params, callback) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(31, params);
		callback && callback(data);
		return data;
	}
	/**
	 * 设置已读
	 * @param {Object} params
	 * 
	 */
	async readNotification(params) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(27, params);

		return data;
	}

	/**
	 * 搜索用户列表的接口。
	 * @param data 输入自己需要的额外参数逻辑，可以不传
	 * @returns {*}
	 */
	async searchUserList(params, callback) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(24, params);
		this.updateUserList(data)
		callback && callback(data);
		return data;
	}

	updateUserList(data) {
		this.userList = data;
	}

	/**
	 * 搜索群组列表的接口。
	 * @param data 输入自己需要的额外参数逻辑，可以不传
	 * @returns {*}
	 */
	async searchGroupList(params, callback) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(25, params);
		callback && callback(data);
		this.updateGroupList(data)
		return data;
	}

	updateGroupList(data) {
		this.groupList = data;
	}
	//不区分大小写
	getUserList(words) {
		if (words) {
			return this.userList.filter(item => item.nick.toLowerCase().includes(words.toLowerCase()));
		} else {
			return this.userList;
		}
	}
	//获取群聊列表数据
	async getGroupListCmdMethods(params, callback) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(35, params);
		callback && callback(data);
		this.updateGroupList(data)
		return data;
	}
	async getGroupInfosCmdMethods(params, callback) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(20, params);
		this.updateGroupInfos(data);
		callback && callback(data);
		return data;
	}

	updateGroupInfos(data) {
		this.groupInfos = data;
	}

	getGroupInfos() {
		return this.groupInfos;
	}

	//退出群聊
	async exitGroupCmdMethods(params, callback) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(43, params);
		callback && callback(data);
		return data;
	}
	//移除出群聊
	async exportGroupCmdMethods(params, callback) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(44, params);
		//移除user
		this.updateGroupInfosMemberList(data);
		callback && callback(data);
		return data;
	}
	//解散群聊
	async disBandGroupCmdMethods(params, callback){
		const {
			data
		} = await uni.$Im.executeCmdMethods(42, params);
		callback && callback(data);
		return data;
	}

	updateGroupInfosMemberList(data) {
		this.groupInfos.memberList.splice(this.groupInfos.memberList.findIndex(item => item.userId == data.users[0]), 1)
	}
	/**
	 * 获取群组在线用户列表请求; 【 在线：代表绑定群组，并且进入了群组 】
	 * @param groupId
	 * @returns {*|Promise<unknown>}
	 */
	async getGroupOnlineUsersCmdMethods(params, callback) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(21, params);
		callback && callback(data);
		return data;
	}
	getGroupList(words) {
		if (words) {
			return this.groupList.filter(item => item.name.toLowerCase().includes(words.toLowerCase()));
		} else {
			return this.groupList;
		}
	}

	async addFriendInvite(params, callback) {
		const body = await uni.$Im.executeCmdMethods(29, params);
		const {
			data
		} = body;
		callback && callback(body);
		return data;
	}

	async addGroupInvite(params, callback) {
		const body = await uni.$Im.executeCmdMethods(36, params);
		const {
			data
		} = body;
		callback && callback(body);
		return data;
	}

	//创建群聊
	async createGroup(params, callback) {
		const body = await uni.$Im.executeCmdMethods(39, params);
		const {
			data
		} = body;
		callback && callback(body);
		return data;
	}

	// 获取好友列表
	get(words) {
		const friendsList = this.friendsList.filter(item=> item.userId != uni.$Im.getCurrentUserId()); 
		if (words) {
			return friendsList.filter(item => item.nick.toLowerCase().includes(words.toLowerCase()));
		} else {
			return friendsList;
		}
	}

	set(data) {
		this.friendsList = data;
	}
}
export default Friends